package com.chain.BrotherExchange.ui.fragment;

import android.app.Activity;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chain.BrotherExchange.R;
import com.chain.BrotherExchange.data.Resource;
import com.chain.BrotherExchange.event.LoginEvent;
import com.chain.BrotherExchange.event.LogoutEvent;
import com.chain.BrotherExchange.event.UserNicknameChangeEvent;
import com.chain.BrotherExchange.event.UserPortraitChangeEvent;
import com.chain.BrotherExchange.ui.activity.AboutActivity;
import com.chain.BrotherExchange.ui.activity.ExtractAddressManageActivity;
import com.chain.BrotherExchange.ui.activity.MyAssetsActivity;
import com.chain.BrotherExchange.ui.activity.MyInvitationCodeActivity;
import com.chain.BrotherExchange.ui.activity.MyOrderActivity;
import com.chain.BrotherExchange.ui.activity.MyTransactionOrderActivity;
import com.chain.BrotherExchange.ui.activity.PaymentActivity;
import com.chain.BrotherExchange.ui.activity.PersonalCenterActivity;
import com.chain.BrotherExchange.ui.activity.SecurityCenterActivity;
import com.chain.BrotherExchange.ui.activity.TradingQualificationActivity;
import com.chain.BrotherExchange.ui.core.AbsNetworkStateFragment;
import com.chain.BrotherExchange.ui.listener.LoginInterceptListener;
import com.chain.BrotherExchange.utils.ActivityBarHelper;
import com.chain.BrotherExchange.utils.Androids;
import com.chain.BrotherExchange.utils.ImageViews;
import com.chain.BrotherExchange.viewmodel.MainViewModel;
import com.chain.BrotherExchange.viewmodel.PersonalCenterViewModel;
import com.chain.BrotherExchange.viewmodel.core.ViewModelProviders;
import com.example.common.update.AppDialogDownloadView;
import com.meiqia.meiqiasdk.util.MQIntentBuilder;

import org.greenrobot.eventbus.Subscribe;

import butterknife.Bind;

public class MyFragment extends AbsNetworkStateFragment {

    @Bind(R.id.set_layout) View mSetLayout;
    @Bind(R.id.uid_text_view) TextView mUidText;
    @Bind(R.id.about_us_layout) View mAboutLayout;
    @Bind(R.id.my_asset_layout) View mMyAssetLayout;
    @Bind(R.id.my_order_layout) View mMyOrderLayout;
    @Bind(R.id.mode_payment_layout) View mPaymentLayout;
    @Bind(R.id.invitation_layout) View mInvitationLayout;
    @Bind(R.id.nickname_text_view) TextView mNicknameText;
    @Bind(R.id.version_update_view) View mVersionUpdateView;
    @Bind(R.id.user_avatar_image) ImageView mUserAvatarImage;
    @Bind(R.id.security_center_layout) View mSecurityCenterLayout;
    @Bind(R.id.personal_center_layout) View mPersonalCenterLayout;
    @Bind(R.id.contact_service_layout) View mContactServiceLayout;
    @Bind(R.id.my_transaction_order_layout) View mMyTransactionOrderLayout;
    @Bind(R.id.extract_coin_address_layout) View mExtractAddressManageLayout;

    private PersonalCenterViewModel mViewModel;

    public static Fragment newInstance() {
        return new MyFragment();
    }

    @Override
    protected int getLayoutID() {
        return R.layout.fragment_my;
    }

    @Override
    protected void initActions() {
        mViewModel = ViewModelProviders.of(this).get(PersonalCenterViewModel.class);

        // 加载用户信息
        initUserInfo();

        // 我的订单
        mMyOrderLayout.setOnClickListener(new LoginInterceptListener(v ->
                MyOrderActivity.launch(v.getContext())
        ));

        // 锁仓
        mSetLayout.setOnClickListener(new LoginInterceptListener(v ->
                TradingQualificationActivity.launch(v.getContext())
        ));

        // 我的资产
        mMyAssetLayout.setOnClickListener(new LoginInterceptListener(v ->
                MyAssetsActivity.launch(v.getContext())
        ));

        // 我的邀请码
        mInvitationLayout.setOnClickListener(new LoginInterceptListener(v ->
                MyInvitationCodeActivity.launch(v.getContext())
        ));

        // 安全中心
        mSecurityCenterLayout.setOnClickListener(new LoginInterceptListener(v ->
                SecurityCenterActivity.launch(v.getContext())
        ));

        // 个人中心
        mPersonalCenterLayout.setOnClickListener(new LoginInterceptListener(v ->
                PersonalCenterActivity.launch(v.getContext())
        ));

        // 我的交易单
        mMyTransactionOrderLayout.setOnClickListener(new LoginInterceptListener(v ->
                MyTransactionOrderActivity.launch(v.getContext())
        ));

        // 联系客服
        mContactServiceLayout.setOnClickListener(v -> startCustomerServices());

        // 关于我们
        mAboutLayout.setOnClickListener(v -> AboutActivity.launch(v.getContext()));

        // 支付方式
        mPaymentLayout.setOnClickListener(new LoginInterceptListener(v ->
                PaymentActivity.launch(v.getContext()))
        );

        // 提币管理
        mExtractAddressManageLayout.setOnClickListener(new LoginInterceptListener(v ->
                ExtractAddressManageActivity.launch((Activity) v.getContext())
        ));

        // 版本更新提示
        MainViewModel mainViewModel = ViewModelProviders.of(this).get(MainViewModel.class);
        mainViewModel.updateInfo().observe(this, updateInfo -> {
            if (AppDialogDownloadView.isDisplayUpdateHint(getActivity(), updateInfo.newVersion)) {
                mVersionUpdateView.setVisibility(View.VISIBLE);
            }
        });
    }

    private void initUserInfo() {
        mViewModel.userInfo().observe(this, resource -> {
            Resource.load(getContext(), resource, this, result -> {
                ImageViews.load(result.getHead_scul(), mUserAvatarImage);
                mNicknameText.setText(result.getNick_name());
                mUidText.setText("UID: " + result.getUid());
            });
        });
    }

    @Override
    protected boolean useEventBus() {
        return true;
    }

    private void startCustomerServices() {
        startActivity(new MQIntentBuilder(getContext()).build());
    }

    @Override
    protected void onVisible() {
        ActivityBarHelper.setTransparentStatus(getActivity());
    }

    @Subscribe
    public void loginEvent(LoginEvent event) {
        ImageViews.load(event.getPortrait(), mUserAvatarImage);
        mNicknameText.setText(event.getNickname());
        mUidText.setText("UID: " + Androids.LocalConfig.getUid());
    }

    @Subscribe
    public void logoutEvent(LogoutEvent event) {
        // TODO 写法可以改进
        mUidText.setText(R.string.welcome_app);
        mNicknameText.setText(R.string.please_login);
        mUserAvatarImage.setImageResource(R.drawable.ic_placeholder_avatars);
    }

    @Subscribe
    public void userNicknameChange(UserNicknameChangeEvent event) {
        mNicknameText.setText(event.nickname);
    }

    @Subscribe
    public void userInfoChangeEvent(UserPortraitChangeEvent event) {
        ImageViews.load(event.portrait, mUserAvatarImage);
    }

    @Override
    public void onAnewRequest() {
        initUserInfo();
    }
}
